package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StArtProcessRelationBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StArtProcessRelationVo;
import com.ruoyi.ruoyiyinzheng.domain.StArtProcessRelation;
import com.ruoyi.ruoyiyinzheng.mapper.StArtProcessRelationMapper;
import com.ruoyi.ruoyiyinzheng.service.IStArtProcessRelationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工艺与工序关联Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@RequiredArgsConstructor
@Service
public class StArtProcessRelationServiceImpl implements IStArtProcessRelationService {

    private final StArtProcessRelationMapper baseMapper;

    /**
     * 查询工艺与工序关联
     */
    @Override
    public StArtProcessRelationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工艺与工序关联列表
     */
    @Override
    public TableDataInfo<StArtProcessRelationVo> queryPageList(StArtProcessRelationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StArtProcessRelation> lqw = buildQueryWrapper(bo);
        Page<StArtProcessRelationVo> result = baseMapper.selectStArtProcessPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工艺与工序关联列表
     */
    @Override
    public List<StArtProcessRelationVo> queryList(StArtProcessRelationBo bo) {
        LambdaQueryWrapper<StArtProcessRelation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectStArtProcessVoList(lqw);
    }

    private LambdaQueryWrapper<StArtProcessRelation> buildQueryWrapper(StArtProcessRelationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StArtProcessRelation> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getArtName() != null, StArtProcessRelation::getArtName, bo.getArtName());
        lqw.like(bo.getProcessName() != null, StArtProcessRelation::getProcessName, bo.getProcessName());
        lqw.eq(StringUtils.isNotBlank(bo.getStep()), StArtProcessRelation::getStep, bo.getStep());
        return lqw;
    }

    /**
     * 新增工艺与工序关联
     */
    @Override
    public Boolean insertByBo(StArtProcessRelationBo bo) {
        StArtProcessRelation add = BeanUtil.toBean(bo, StArtProcessRelation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工艺与工序关联
     */
    @Override
    public Boolean updateByBo(StArtProcessRelationBo bo) {
        StArtProcessRelation update = BeanUtil.toBean(bo, StArtProcessRelation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StArtProcessRelation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工艺与工序关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
